package flex.cc.store.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import flex.cc.PublicUtil;
import flex.cc.store.dto.entity.StoreCouponEntity;
import flex.cc.store.enums.ActivityNameEnum;
import flex.cc.store.enums.CouponStatusEnum;
import flex.cc.store.service.CardExportService;
import flex.cc.store.service.StoreCouponService;
import flex.cc.util.ExportUtil;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;

import java.util.List;

/**
 * @author KongShiYu
 * @create 2022/1/5
 */
@Service("cardExportService")
public class CardExportServiceImpl implements CardExportService {

    @Resource
    private StoreCouponService storeCouponService;

    /**
     * @desc 卡券导出 KSY
     * @param flag : 1部分导出；2全部导出
     * @return
     */
    @SuppressWarnings("deprecation")
    @Override
    public void downLoadSelection(HttpServletRequest request, HttpServletResponse response, StoreCouponEntity storeCouponVo, String fileParentPath, String fileName, String flag) {
        // 表名、表头数据
        String title = fileName;
        String[] headers = {"卡券码","服务类别","服务项目","渠道方","服务商","卡券状态","发送时间","卡券生效时间","卡券失效时间","核销时间","归属服务包编码","归属服务包名称","绑定车牌号","创建时间"};
        String[] fields = {"couponNum","serviceType","activityId","channel","server","status","sendDate","cardEffDate","cardExpDate","cardcheckDate","packageCode","packageName","carNo","cardCreateTime"};

        IPage<StoreCouponEntity> page = null;
        if("1".equals(flag)){
            page = storeCouponService.queryCardListWithDataIds(storeCouponVo);
        }else{
            page = storeCouponService.queryCardListWithPage(storeCouponVo);
        }
        List<StoreCouponEntity> records = page.getRecords();
        for(int i=0; i<records.size(); i++){
            StoreCouponEntity storeCouponEntity = records.get(i);
            //服务类别：服务包、单项服务
            if(PublicUtil.isNotEmpty(storeCouponEntity.getPackageName())){
            	storeCouponEntity.setServiceType("服务包");
            }else{
            	storeCouponEntity.setServiceType("单项服务");
            }
//            //卡券类型，服务项目
//            if(PublicUtil.isNotEmpty(storeCouponEntity.getActivityId())){
//                storeCouponEntity.setActivityId(ActivityNameEnum.getName(storeCouponEntity.getActivityId()));
//            }
            //卡券状态
            if(PublicUtil.isNotEmpty(storeCouponEntity.getStatus())){
                storeCouponEntity.setStatus(CouponStatusEnum.getName(storeCouponEntity.getStatus()));
            }
            storeCouponEntity.setChannel( storeCouponEntity.getChannel().equals("ZXHY") ? "阳光" : (storeCouponEntity.getChannel().equals("GHMY") ? "国华" : storeCouponEntity.getChannel()) );
            storeCouponEntity.setCardcheckDate( PublicUtil.isNotEmpty(storeCouponEntity.getCardcheckDate()) ? storeCouponEntity.getCardcheckDate() : "" );
            storeCouponEntity.setPackageCode( PublicUtil.isNotEmpty(storeCouponEntity.getPackageCode()) ? storeCouponEntity.getPackageCode() : "" );
            storeCouponEntity.setPackageName( PublicUtil.isNotEmpty(storeCouponEntity.getPackageName()) ? storeCouponEntity.getPackageName() : "" );
            storeCouponEntity.setCarNo( PublicUtil.isNotEmpty(storeCouponEntity.getCarNo()) ? storeCouponEntity.getCarNo() : "" );
        }
        if (!PublicUtil.isEmpty(records)) {
            try {
                SXSSFWorkbook wb = ExportUtil.createWorkbook(10, false);//创建工作簿对象
                ExportUtil.exportExcel(title, headers, fields, 0, wb, records);
                File fileParent = new File(fileParentPath);
                if (!fileParent.exists()) { // 若目录不存在则创建
                    fileParent.mkdirs();
                }

                File file = new File(fileParentPath + File.separator + fileName + ".xlsx");
                if (file.exists()) { // 若文件已存在则删除
                    file.delete();
                }
                try {
                    wb.write(new FileOutputStream(file));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
